package com.zsoft.webmail.servlet.hainannet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.zsoft.webmail.*;
import com.zsoft.webmail.Application;
import comm.util.Utils;
import com.zsoft.webmail.folder.*;

public class MailMoveServlet extends HttpServlet {
  //private static final String CONTENT_TYPE = Application.getProperty("ContentType");
  private static final String CONTENT_TYPE = "text/html";
  /**Initialize global variables*/
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }
  /**Process the HTTP Get request*/
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    comm.http.Request request1=new comm.http.Request(request,Application.getProperty("defaultInputCharset"));

    //PrintWriter out = response.getWriter();
    String mimeCharset1=Application.getProperty("defaultMimeCharset");
    if(mimeCharset1==null || mimeCharset1.equals(""))
    mimeCharset1 = com.zsoft.webmail.mimeutility.MimeUtility.mimeCharset(com.zsoft.webmail.mimeutility.MimeUtility.getDefaultJavaCharset());
    String javaCharset1=com.zsoft.webmail.mimeutility.MimeUtility.javaCharset(mimeCharset1);
    comm.http.ResponseOut out= new comm.http.ResponseOut(response,javaCharset1,false);


    MemberAuthorization memberAuthToken=null ;
    HttpSession session=request.getSession();
    Object object = session.getAttribute("authBean");
    if(object==null){
      session.setAttribute("authBean",new com.zsoft.webmail.AuthBean(session.getId(),request.getRemoteAddr()));
      response.sendRedirect( "login.jsp" );
      return;
    }else{
      memberAuthToken = ((AuthBean)object).getMemberAuthToken();
      if(memberAuthToken==null){
        response.sendRedirect( "login.jsp" );
        return;
      }
    }



out.println("<html>");
out.println("<head>");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset="+mimeCharset1+"\">");
out.println("</head>");
out.println("<body>");

String boxid=request1.getParameter("boxid");
boxid=comm.util.FileUtils.getFilename(boxid);
String destboxid=request1.getParameter("destboxid");
String Inum = request1.getParameter("selall");
int num = java.lang.Integer.parseInt(Inum);

Folder mybox=new Folder(memberAuthToken,boxid);


int flag=0;
Vector vector=new Vector();

for(int i=0; i<num; i++)
{
	if(request1.getParameter("c"+i) != null)
	{
		String mid = request1.getParameter("c"+i);
                mid=comm.util.FileUtils.getFilename(mid);
		vector.add(mid);
	}
}

vector.trimToSize();
String[] msgs=new String[vector.size()];
vector.copyInto(msgs);
try{
mybox.moveMessages(msgs, destboxid);
}catch(Exception e){}

response.sendRedirect("MailList?boxid="+boxid);

out.println("<script>");
out.println("document.location = \"MailList?boxid="+boxid+"\";");
out.println("</script>");
out.println("</body>");
out.println("</html>");

  }
  /**Process the HTTP Post request*/
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
  }
  /**Clean up resources*/
  public void destroy() {
  }
}